<template>
	<view class="bankcard-layout">
		<xh-input title="持卡人" placeholder="" showTitle isSelect v-model="formStore.userRealName" :showRightIcon="false"></xh-input>
		<xh-header title="请绑定法人本人的银行卡"></xh-header>
		 <view class="bankcard-img-box" @tap="showSelectBankCardImage">
		 	<image class="bankcard-img" :src="$uv.test.empty(formStore.picUrl) ? '/static/my/yhk_img.png' : formStore.picUrl" mode="scaleToFill"></image>
		 </view>
		 <xh-header title="请核对信息，若有误请手动更正"></xh-header>
		 <xh-input showTitle title="银行卡号" placeholder="请输入银行卡卡号" v-model="formStore.settleCardNo"></xh-input>
		 <xh-input showTitle title="开户银行" placeholder="请选择" v-model="formStore.bankBranchName" isSelect @didSelect="showSelectBank"></xh-input>
		 <xh-input showTitle title="开户城市" placeholder="请选择省/市" v-model="formStore.settleCardCity" isSelect @didSelect="showSelectCity"></xh-input>
		  <xh-input showTitle title="开户支行" placeholder="请选择" v-model="formStore.bankName" isSelect @didSelect="showSelectBranchBank"></xh-input>
		  <xh-input showTitle title="手机号" placeholder="请输入银行预留手机号" v-model="formStore.phoneNo"></xh-input>
		 <view class="submit-btn" style="margin-top: 50rpx;" @tap="submit">提交</view>
		 <xh-select-city ref="cityRef" @result="selectCityResult"></xh-select-city>
	</view>
</template>

<script setup>
	import { onLoad } from '@dcloudio/uni-app'
	import { ref } from 'vue'
	import { useFormStore,useUserStore } from '@/common/stores'
	import { checkedDebitCard,bindBankCard,agentAuth, addAgent } from '@/common/api/api.js'
	const userStore = useUserStore()
	const formStore = useFormStore()
	const cityRef = ref(null)
	const props = defineProps(['data'])
	const jsonData = JSON.parse(decodeURIComponent(props.data || '{}'))
	// 选择开户银行
	const showSelectBank = ()=>{
		uni.navigateTo({
			url:'/pages/my/auth/bankcard/select-bank/select-bank'
		})
	}
	// 选择支行
	const showSelectBranchBank = ()=>{
		if(uni.$uv.test.empty(formStore.bankBranchName)){
			uni.$utils.showToast('请选择开户银行')
			return
		}
		else if(uni.$uv.test.empty(formStore.settleCardCityCode)){
			uni.$utils.showToast('请选择开户城市')
			return
		}
		uni.navigateTo({
			url:'/pages/my/auth/bankcard/branch-bank/branch-bank'
		})
	}
	// 选择城市
	const showSelectCity = ()=>{
		cityRef.value.open()
	}
	// 选择城市结果回调
	const selectCityResult = (res)=>{
		formStore.settleCardProvince = res.provinceName
		formStore.settleCardCity = res.cityName
		formStore.settleCardProvinceCode = res.provinceCode
		formStore.settleCardCityCode = res.cityCode
		formStore.bankName = ''
		formStore.bankNo = ''
	}
	// 选择银行卡图片
	const showSelectBankCardImage = async ()=>{
		const res = await uni.$utils.uploadImage()
		formStore.picUrl = res
		const ocrRes = await uni.$utils.getImageOcrInfo(res,'bankcard')
		console.log(ocrRes)
		if(!uni.$uv.test.empty(ocrRes.ocr)){
			formStore.settleCardNo = ocrRes.ocr.bankCardNo
		}
	}
	// 校验银行卡号是否是借记卡
	const checkedBankCard = async ()=>{
		const res = await checkedDebitCard({cardNo:uni.$uv.trim(formStore.settleCardNo,'all')})
		const cardType = res.result.cardType
		if(cardType !== '01'){
			uni.$utils.showToast('请绑定借记卡')
		}
		else{
			if(uni.$uv.test.empty(jsonData)){
				submitAgentAuth()
			}
			else{
				submitSettleCardChangeBind()
			}
		}
	}
	
	// 提交
	const submit = async () => {
	    if (uni.$uv.test.empty(formStore.picUrl)) {
			uni.$utils.showToast('请上传银行卡卡号面照片')
			return
		}
		else if (uni.$uv.test.empty(uni.$uv.trim(formStore.settleCardNo, 'all'))) {
			uni.$utils.showToast('请输入银行卡卡号')
			return
		} else if (uni.$uv.test.empty(formStore.bankBranchName)) {
			uni.$utils.showToast('请选择开户银行')
			return
		} else if (uni.$uv.test.empty(formStore.settleCardCity)) {
			uni.$utils.showToast('请选择开户城市')
			return
		} else if (uni.$uv.test.empty(formStore.bankName)) {
			uni.$utils.showToast('请选择开户支行')
			return
		}
		else if (!uni.$uv.test.mobile(formStore.phoneNo)) {
			uni.$utils.showToast('请输入正确的银行预留手机号')
			return
		}
		checkedBankCard()
	}
	
	// 提交结算卡换绑
	const submitSettleCardChangeBind = async ()=>{
		let params = {}
		params.settleType = '1'
		params.settleName = formStore.userRealName
		params.picUrl = formStore.picUrl
		params.bankAddress = formStore.settleCardCityCode
		params.settleCardNo = uni.$uv.trim(formStore.settleCardNo, 'all')
		params.phoneNo =  uni.$uv.trim(formStore.phoneNo, 'all')
		if (!uni.$uv.test.empty(formStore.bankName)) {
			params.bankName = formStore.bankName
		}
		if (!uni.$uv.test.empty(formStore.bankNo)) {
			params.bankNo = formStore.bankNo
		}
		if (!uni.$uv.test.empty(formStore.bankBranchNo)) {
			params.parentBankNo = formStore.bankBranchNo
		}
		if (!uni.$uv.test.empty(formStore.bankBranchName)) {
			params.bankBranch = formStore.bankBranchName
		}
		if (!uni.$uv.test.empty(jsonData)) {
			params.settleId = jsonData.id
			params.changeFlag = '1'
		}
		if (!uni.$uv.test.empty(formStore.bankIcon)) {
			params.bankIcon = formStore.bankIcon
		}
		await bindBankCard(params)
		uni.$utils.showToast('绑定成功')
		formStore.$reset()
		uni.navigateBack()
		
	}
	// 提交代理商认证
	const submitAgentAuth = async ()=>{
		let params = {}
		params.frontUrl = formStore.frontPhoto
		params.backUrl = formStore.reversePhoto
		params.userName = formStore.userRealName
		params.certNo = formStore.identityNo
		if(formStore.certType == '1'){
			params.certType = '1'
			params.certTime = '2199-12-30'
		}
		else{
			params.certType = '0'
			params.certTime = formStore.endCertTime
		}
		if(formStore.agentType == '个人'){
			params.agentType = '01'
		}
		else{
			params.agentType = '02'
			params.licenseUrl = formStore.licensePhoto
			params.businessLicenseNo = formStore.businessLicenseNo
			params.businessName = formStore.businessName
			if(formStore.businessTimeType == '1') {
				params.businessTime = '2199-12-30'
			}
			else{
				params.businessTime = formStore.businessTime
			}
		}
		let settleCardParams = {}
		settleCardParams.picUrl = formStore.picUrl
		settleCardParams.settleCardNo = uni.$uv.trim(formStore.settleCardNo,'all')
		settleCardParams.settleType = '1'
		settleCardParams.settleName = formStore.userRealName
		settleCardParams.bankAddress = formStore.settleCardCityCode
		settleCardParams.phoneNo =  uni.$uv.trim(formStore.phoneNo, 'all')
		if (!uni.$uv.test.empty(formStore.bankName)) {
			settleCardParams.bankName = formStore.bankName
		}
		if (!uni.$uv.test.empty(formStore.bankNo)) {
			settleCardParams.bankNo = formStore.bankNo
		}
		if (!uni.$uv.test.empty(formStore.bankBranchNo)) {
			settleCardParams.parentBankNo = formStore.bankBranchNo
		}
		if (!uni.$uv.test.empty(formStore.bankBranchName)) {
			settleCardParams.bankBranch = formStore.bankBranchName
		}
		params.settleInfo = JSON.stringify(settleCardParams)
		if(!uni.$uv.test.empty(formStore.contactNo)){
			//新增代理商认证
			params.contactNo = formStore.contactNo
			params.smsCode = formStore.smsCode
			await addAgent(params)
			formStore.$reset()
			uni.navigateTo({
				url:'/pages/my/auth/result/result'
			})
		}
		else{
			//代理商认证
			await agentAuth(params)
			formStore.$reset()
			uni.navigateTo({
				url:'/pages/my/auth/result/result'
			})
		}
	}
	onLoad(()=>{
		if(uni.$uv.test.empty(formStore.userRealName)){
			formStore.userRealName = userStore.user.userRealName
		}
	})
</script>

<style lang="scss" scoped>
	.bankcard-layout {
		padding: 20rpx 0 30rpx;
		.bankcard-img-box{
			background-color: #ffffff;
			padding: 30rpx;
			.bankcard-img{
				width: 100%;
				height: 270rpx;
			}
		}
	}
</style>